package cn.zktr.Personnel.model.service.zhouyongcong.end;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.zktr.Personnel.model.dao.zhouyongcong.PmsStmDao;
import cn.zktr.Personnel.model.pojos.PerPmsDis;
import cn.zktr.Personnel.model.pojos.PerPmsEmp;
import cn.zktr.Personnel.model.pojos.PerPmsStm;
import cn.zktr.Personnel.model.pojos.PerPmsYG;
import cn.zktr.Personnel.model.pojos.PerSlyYxz;
import cn.zktr.Personnel.model.service.zhouyongcong.findService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveAuditService;
import cn.zktr.Personnel.model.service.zhouyongcong.savePojosService;
import cn.zktr.Personnel.model.service.zhouyongcong.saveService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitObj;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitfindService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitgetService;
import cn.zktr.Personnel.model.service.zhouyongcong.monit.monitsaveService;

@Service
@Transactional(rollbackFor=Exception.class)
public class separationService {
	
	@Autowired
	private findService fs;
	@Autowired
	private saveService ss;
	@Autowired
	private saveAuditService sas;
	@Autowired
	private savePojosService sps;
	@Autowired
	private PmsStmDao ptd;
	@Autowired
	private monitsaveService mss;
	@Autowired
	private monitfindService mfs;
	@Autowired
	private monitgetService mgs;
	
	public void dleStm(Integer empid) {
		ptd.delete(fs.findEmpid(empid).getPerPmsStms().get(0));
	}
	
	public monitObj setMo1(Integer empid,String disyy) {
		PerPmsEmp ppe = fs.findEmpid(empid);
		monitObj mo1 = new monitObj(true,false);
		mo1.setOld_obj(sas.save_Dis(empid,ppe.getPerPmsStms().get(0).getPerPmsDept(),disyy,"复职"));
		mo1.setNew_obj(sas.save_Dis(empid,ppe.getPerPmsStms().get(0).getPerPmsDept(),disyy,"已离职"));
		return mo1;
	}
	
	public monitObj setMo2(Integer empid) {
		PerPmsEmp ppe = fs.findEmpid(empid);
		PerPmsStm pps1 = fs.findStmid(empid);
		PerPmsStm pps2 = new PerPmsStm();
		pps2.setStmid(ppe.getPerPmsStms().get(0).getStmid());
		monitObj mo2 = new monitObj(false,true);
		mo2.setOld_obj(pps1);
		mo2.setNew_obj(pps2);
		return mo2;
	}
	
	public monitObj setMo3(Integer empid) {
		PerPmsEmp ppe = fs.findEmpid(empid);
		monitObj mo3 = new monitObj(false,false);
		for(PerPmsYG ppyg1:ppe.getPerPmsYGs()) {
			if(ppyg1.getPerPmsFun().getFunname().equals("登录")) {
				mo3.setOld_obj(ppyg1);
				PerPmsYG ppyg2 = ppyg1;
				ppyg2.setYgwht("false");
				mo3.setNew_obj(ppyg2);
			}
		}
		return mo3;
	}
	
	public monitObj setMo4(Integer empid) {
		System.err.println(empid);
		PerSlyYxz psy = fs.findEmpid(empid).getPerSlyYxzs().get(0);
		mgs.getPojos(psy);
		PerSlyYxz psyx = new PerSlyYxz();
		System.err.println("Yxz:"+psy.getYxzid());
		psyx.setYxzid(psy.getYxzid());
		monitObj mo4 = new monitObj(false,true);
		mo4.setOld_obj(psy);
		mo4.setNew_obj(psyx);
		System.err.println("瞎比比"+mo4.getNew_obj());
		return mo4;
	}
	
	public void adddis(Integer empid,String disyy,boolean boo) {
		System.err.println("empid:"+empid+"dis.size:"+fs.findEmpid(empid).getPerPmsDis().size());
		if(fs.findEmpid(empid).getPerPmsDis().size()==0) {
			PerPmsEmp ppe = fs.findEmpid(empid);
			
			monitObj mo1 = setMo1(empid, disyy);	//离职表数据添加
			monitObj mo2 = setMo2(empid);			//职员表员工绑定删除
			monitObj mo3 = setMo3(empid);			//权限表登录权限限制
			monitObj mo4 = setMo4(empid);
			monitObj[] mos = {mo1,mo2,mo3,mo4};
			
			ss.save_Audit_id(4,empid,"辞职信!", disyy);
			
			mss.save(empid,mos,4);
			
			PerPmsDis ppd0 = sas.save_Dis(empid,ppe.getPerPmsStms().get(0).getPerPmsDept(),disyy,"待审核");
			sps.modif_Dis(ppd0);
		}
	}
	
	public void adddis(Integer empid,String disyy) {
		if(fs.findEmpid(empid).getPerPmsDis().size()==0) {
			PerPmsEmp ppe = fs.findEmpid(empid);
			
			monitObj mo1 = setMo1(empid, disyy);	//离职表数据添加
			monitObj mo2 = setMo2(empid);			//职员表员工绑定删除
			monitObj mo3 = setMo3(empid);			//权限表登录权限限制
			monitObj[] mos = {mo1,mo2,mo3};
			
			ss.save_Audit_id(4,11,empid,"辞职信!", fs.findEmpid(empid).getEmpname()+"申请辞职,所在岗位："+fs.findEmpid(empid).getPerPmsStms().get(0).getPerPmsPot().getPotzw());
			
			mss.save(empid,mos,4);
			
			PerPmsDis ppd0 = sas.save_Dis(empid,ppe.getPerPmsStms().get(0).getPerPmsDept(),disyy,"待审核");
			sps.modif_Dis(ppd0);
		}
	}
	
	public void fuzhi(Integer disid) {
		PerPmsDis ppd = fs.findDisid(disid);
		if(ppd.getPerPmsEmp().getPerAudits1().size()!=0) {
			mfs.dnif(ppd.getPerPmsEmp().getPerAudits1().get(ppd.getPerPmsEmp().getPerAudits1().size()-1).getPerAuditMdies());
		};
	}
}
